package com.jk.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jk.dao.TemPositionMapper;
import com.jk.dto.TemPosition;
import com.jk.dto.TemViewer;
import com.jk.pojo.PagedResponse;
import com.jk.service.TemPositionService;
import com.jk.utils.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yzf
 * @version 1.0
 * @ClassName TemPositionServiceImpl
 * @program: office
 * @description: TODO
 * @date 2025/2/13 10:42
 */
@Service
public class TemPositionServiceImpl implements TemPositionService {

    @Autowired
    private TemPositionMapper temPositionMapper;

    @Override
    public PagedResponse<TemPosition> findTemList(TemPosition temPosition, Integer pageNum, Integer pageSize) {
        Page<TemPosition> page = new Page<>(pageNum,pageSize);
        IPage<TemPosition> ipage = temPositionMapper.findTemList(temPosition,page);
        return PaginationUtil.getPaginationResponse(ipage.getRecords(),pageNum,pageSize, ipage.getTotal());
    }

    //删除
    @Override
    public void delTemById(Integer id) {
        temPositionMapper.deleteById(id);

        temPositionMapper.delTemViewerById(id);
    }

    //新增
    @Override
    public void addTem(TemPosition temPosition) {
        temPositionMapper.insert(temPosition);

        for (TemViewer temViewer : temPosition.getTemViewers()) {
            temViewer.setConferenceRoom(temViewer.getConferenceRoom());
            temViewer.setPostsId(temPosition.getId());
            temViewer.setInterviewer(temViewer.getInterviewer());
            temPositionMapper.addTemViewer(temViewer);
        }
    }

    //大修改
    @Override
    public void updateTem(TemPosition temPosition) {
        temPositionMapper.updateById(temPosition);
    //先删
        temPositionMapper.delTemViewerById(temPosition.getId());
    //后增
        for (TemViewer temViewer : temPosition.getTemViewers()) {
            temViewer.setConferenceRoom(temViewer.getConferenceRoom());
            temViewer.setPostsId(temPosition.getId());
            temViewer.setInterviewer(temViewer.getInterviewer());
            temPositionMapper.addTemViewer(temViewer);
        }
    }

    //修改为招聘中
    @Override
    public void updateStatus(Integer id) {
        temPositionMapper.updateStatus(id);
    }

    @Override
    public void updateState(TemPosition temPosition) {
        temPositionMapper.updateState(temPosition);
    }

    @Override
    public TemPosition getTem(Integer id) {
        //回显主表
        TemPosition temPosition = temPositionMapper.selectById(id);
        //回显部门表
        List<TemViewer> view = temPositionMapper.getTemById(temPosition.getId());
        temPosition.setTemViewers(view);
        return temPosition;
    }

    @Override
    public List<TemPosition> findTemPosition(TemPosition temPosition) {
        return temPositionMapper.selectList(null);
    }
}
